﻿namespace KeRui.Upload
{
    using System;
    using System.IO;
    using System.Reflection;

    internal class TempFile
    {
        private string mPath = null;
        private System.IO.FileStream mStream = null;
        private static string sTempPath;

        public void CloseStream()
        {
            if (this.mStream != null)
            {
                try
                {
                    this.mStream.Close();
                }
                catch (Exception exception)
                {
                    try
                    {
                        Log.UniqueError("Unable to close temporary file \"", this.mPath + "\". " + exception.Message);
                    }
                    catch
                    {
                    }
                }
                this.mStream = null;
            }
        }

        private void DeleteFile()
        {
            if (this.mPath != null)
            {
                try
                {
                    File.Delete(this.mPath);
                }
                catch (Exception exception)
                {
                    try
                    {
                        if (File.Exists(this.mPath))
                        {
                            Log.UniqueError("Unable to delete temporary file \"", this.mPath + "\". " + exception.Message);
                        }
                    }
                    catch
                    {
                    }
                }
                this.mPath = null;
            }
        }

        public string Detach()
        {
            string mPath = this.mPath;
            this.CloseStream();
            this.mPath = null;
            return mPath;
        }

        public void Dispose()
        {
            try
            {
                this.CloseStream();
                this.DeleteFile();
            }
            catch
            {
            }
        }

        ~TempFile()
        {
            this.Dispose();
        }

        private static string GetTempFileName()
        {
            Guid guid = Guid.NewGuid();
            return (GetTempPath() + guid.ToString() + ".txt");
        }

        private static string GetTempPath()
        {
            lock (typeof(TempFile))
            {
                if ((sTempPath == null) || (sTempPath == ""))
                {
                    sTempPath = System.IO.Path.GetTempPath() + Assembly.GetExecutingAssembly().GetName().Name + @"\";
                    try
                    {
                        Directory.Delete(sTempPath, true);
                    }
                    catch
                    {
                    }
                    try
                    {
                        Directory.CreateDirectory(sTempPath);
                    }
                    catch (Exception exception)
                    {
                        Log.UniqueError("Unable to create temporary directory \"", sTempPath + "\". " + exception.Message);
                        sTempPath = System.IO.Path.GetTempPath();
                    }
                }
            }
            return sTempPath;
        }

        public static void SetTempPath(string inPath, bool inTest)
        {
            if (inPath.Length > 0)
            {
                if (inPath[inPath.Length - 1] != '\\')
                {
                    inPath = inPath + @"\";
                }
                inTest = inTest && (sTempPath != inPath);
                sTempPath = inPath;
                if (inTest)
                {
                    TempFile file = new TempFile();
                    System.IO.FileStream fileStream = file.FileStream;
                }
            }
        }

        public System.IO.FileStream FileStream
        {
            get
            {
                if (this.mStream == null)
                {
                    this.mStream = new System.IO.FileStream(this.Path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                }
                return this.mStream;
            }
        }

        public string Path
        {
            get
            {
                if (this.mPath == null)
                {
                    this.mPath = GetTempFileName();
                }
                return this.mPath;
            }
        }

        public bool Writable
        {
            get
            {
                System.IO.FileStream fileStream = null;
                try
                {
                    fileStream = this.FileStream;
                }
                catch
                {
                }
                return (fileStream != null);
            }
        }
    }
}

